package com.spsy.controller;

import com.alibaba.fastjson.JSON;
import com.spsy.common.core.domain.R;
import com.spsy.domain.Orders;
import com.spsy.pay.api.PayApi;
import com.spsy.service.IOrderService;
import com.spsy.vo.AddOrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

import static com.spsy.common.core.utils.PageUtils.startPage;
import static com.spsy.common.core.web.domain.AjaxResult.success;

@RestController
@RequestMapping("order")
@Slf4j
public class OrderOptsController {


    @Autowired
    private PayApi payApi;

    @Autowired
    private IOrderService orderService;




    @PostMapping(value = "create", consumes = MediaType.APPLICATION_JSON_VALUE)
    public R create(@RequestBody AddOrderVO addOrderVo) {
        log.info("订单系统-参数 addOrder:{}", JSON.toJSONString(addOrderVo));

        R order = payApi.createOrder(addOrderVo);

        return order;
    }


    @PostMapping(value = "zhifubao", consumes = MediaType.APPLICATION_JSON_VALUE)
    public R zhifubao(@RequestBody AddOrderVO addOrderVo) throws Exception {
        log.info("订单系统-参数 addOrder:{}", JSON.toJSONString(addOrderVo));

        R r = payApi.zhifubao(addOrderVo);
        return r;

    }

    @PostMapping(value = "weixin", consumes = MediaType.APPLICATION_JSON_VALUE)
    public R weixin(@RequestBody AddOrderVO addOrderVo) throws Exception {
        log.info("订单系统-参数 addOrder:{}", JSON.toJSONString(addOrderVo));

        int i = orderService.insertOrder(Orders.builder().orderNo(addOrderVo.getOrderId()).userId(Long.valueOf(addOrderVo.getUserId())).totalAmount(BigDecimal.valueOf(addOrderVo.getPrice())).orderStatus(1).build());

        R weixin = payApi.weixin(addOrderVo);

        if(weixin.getCode() == 200){

        }else{

        }

        return weixin;

    }


}
